package com.example.add8_17;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.add8_17.view.CircleNumberProgress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class XiazaiActivity extends AppCompatActivity {

    private CircleNumberProgress mCircle;
    private Button mBu1;
    private Button mBu2;
    private ProgressBar mPbar;
    private TextView mText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiazai);
        initViews();
    }

    private void initViews() {
        mBu1 = findViewById(R.id.bu1);
        mBu2 = findViewById(R.id.bu2);
        mPbar = findViewById(R.id.pbar);
        mText = findViewById(R.id.text);
    }
    //  进度
    private int mProgress;
    //  文件保存路径
    private String mSavePath;
    //  判断是否停止
    private boolean mIsCancel = false;
    private void downloadAPK(String apk){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                        String sdPath=Environment.getExternalStorageDirectory()+"/";
                        // 文件保存路径
                        mSavePath=sdPath+"oil";
                        File file = new File(mSavePath);
                        if (!file.exists()){
                            file.mkdir();
                        }

                        // 下载文件
                        HttpURLConnection conn = (HttpURLConnection) new URL(apk).openConnection();
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        int length = conn.getContentLength();

                        File apkFile = new File(mSavePath);
                        FileOutputStream fos = new FileOutputStream(apkFile);

                        int count = 0;
                        byte[] buffer = new byte[1024];
                        while (!mIsCancel){
                            int numread = is.read(buffer);
                            count += numread;
                            // 计算进度条的当前位置
                            mProgress = (int) (((float)count/length) * 100);
                            // 更新进度条
                            mUpdateProgressHandler.sendEmptyMessage(1);

                            // 下载完成
                            if (numread < 0){
                                mUpdateProgressHandler.sendEmptyMessage(2);
                                if (numread==100){
                                    mBu2.setVisibility(View.VISIBLE);
                                }
                                break;
                            }
                            fos.write(buffer, 0, numread);
                        }
                        fos.close();
                        is.close();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @SuppressLint("HandlerLeak")
    private Handler mUpdateProgressHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    // 设置进度条
                    mPbar.setProgress(mProgress);
                    break;
                case 2:
                    // 隐藏当前下载对话框
                   // dialog.dismiss();
                    // 安装 APK 文件
                   // installAPK();
            }
        };
    };
}
